<?php 
	include './header.php';
	$uid=$_SESSION['user']['id'];
	$id=$_GET['id'];
	$model=new Model('order_info');

	//每页多少条
	$num=2;
	//查询总条数
	 $sql2="select count(*) 
		 	from 
			 	`goods` g,
			 	`goods_img` gi,
			 	`order_info` oi 
	 		where 
			    g.id=gi.goods_id
			   and 
			    oi.goods_id=g.id
			   and
			   	gi.is_cover=1
			   and
			   	oi.order_id=$id
			 ";
	
	//总条数
	$total=$model->find($sql2)[0]['count(*)'];
	//var_dump($total);
	//总页数
	$pages=ceil($total/$num);
	//var_dump($pages);
	//获取当前页
	$p=$_GET['p']?$_GET['p']:1;
	//尾页处理
	$p=min($p,$pages);
	//首页处理
	$p=max($p,1);
	//上一页
	$prev=$p-1;
	//下一页
	$next=$p+1;

	//每页开始
	$start=($p-1)*$num;
	//每页结束(偏移量)
	$offset=$num;

	$sql="select 
			g.id 		  gid,
			g.name 		  gname,
			g.price 	  gp,
			g.description gd,
			g.addtime     gtime,
			gi.name 	  giname,
			oi.num 		  oinum 
		   from 
		    goods 		  g,
		    goods_img     gi,
		    order_info	  oi
		   where 
		    g.id=gi.goods_id
		   and 
		    oi.goods_id=g.id
		   and
		   	gi.is_cover=1
		   and
		   	oi.order_id=$id

		   	limit $start,$offset
		  ";
	$goods_list=$model->find($sql);
	//var_dump($goods_list);
	
	$sql1="select c.id id,c.text text,c.comtime comtime,c.status status,u.username uname from `comments` c,`user` u where c.uid=u.id and c.status=1 and c.order_id=$id";
	$model1=new Model('comments');
	$com_list=$model1->find($sql1);
	//var_dump($com_list);

	$str="
		<a href='".URL."/comments.php?id=".$id."' style=color:#ED145B;>首页</a>
		<a href='".URL."/comments.php?p=$prev&id=".$id."' style=color:#ED145B;>上一页</a>
		<a href='".URL."/comments.php?p=$p&id=".$id."' style=color:#ED145B;>$p</a>
		<a href='".URL."/comments.php?p=$next&id=".$id."' style=color:#ED145B;>下一页</a>
		<a href='".URL."/comments.php?p=$pages&id=".$id."' style=color:#ED145B;>尾页</a>
		";
?>
<div class="clear"></div>
<div id="main">
	<div class="goods">
		<div class="person_left">
			<ul>
				<a href="<?php echo URL;?>/personal_info.php"><li><span style="line-height:100px;">个人信息</span></li></a>
				<a href="<?php echo URL;?>/personal_modify.php"><li><span  style="line-height:100px;">修改密码</span></li></a>
				<a href="<?php echo URL;?>/personal_order.php"><li><span  style="line-height:100px;">查看订单</span></li></a>
			</ul>
		</div>
		<div class="person_right">
			<div class="person_main">
				<!--遍历 用户订单商品-->
				<table border="1" cellpadding="5" cellspacing="0" width="92%" style="margin:20px auto;text-align:center;margin-top:50px;">
					<tr height="60px;">
						<th>商品编号</th>
						<th>商品名称</th>
						<th>商品图片</th>
						<th>商品描述</th>
						<th>单价</th>
						<th>数量</th>
						<th>小计</th>
						<th>添加时间</th>
					</tr>
					<?php foreach ($goods_list as $key => $value): ?>
						<tr height="60">
							<td><?php echo $value['gid'];?></td>
							<td><?php echo $value['gname'];?></td>
							<td><img src="<?php echo URL.'/public/uploads/'.substr($value['giname'],0,4).'/'.substr($value['giname'], 4,2).'/'.substr($value['giname'],6,2).'/50_'.$value['giname'];?>" alt="美工回家生娃了"/></td>
							<td><?php echo $value['gd'];?></td>
							<td><font style="color:red;">¥:<?php echo $value['gp'];?></font></td>
							<td><?php echo $value['oinum'];?></td>
							<td><font style="color:red;">¥:<?php echo $value['gp']*$value['oinum'];?></font></td>
							<td><?php echo date('Y年m月d日',$value['gtime']);?></td>
						</tr>
					<?php endforeach ?>
					<tr>
						<th colspan="8" height="50px">
							<?php echo $str;?>
						</th>
					</tr>
				</table>
				<div class="comments">
					<div class="top">
						评论：
					</div>
					<?php if (!empty($com_list)) :?>
						<?php foreach ($com_list as $k => $v) :?>
							<li>编号：<?php echo $v['id'];?></li>
							<li>评论者：<?php echo $v['uname'];?></li>
							<li>评论时间：<?php echo date('Y年m月d日',$v['comtime']);?></li>
							<li><?php echo $v['text'];?></li>
						<?php endforeach;?>
						<?php else :?>
							<div>
								<form action="<?php echo URL;?>/dopersonal.php?a=comment" method="post">
									<input type="hidden" name="id" value="<?php echo $id;?>">
									<textarea name="text" style="width:200px;height:100px;">			
									</textarea><br/>
									<br/>
									<button style="width:80px;height:30px;border:none;background:#C40000;border-radius:10px;">提交</button>
								</form>
							</div>
					<?php endif;?>
				</div>
			</div> 
		</div>
	</div>
</div>
<div class="clear"></div>
<?php 
	include PATH.'/footer.php';
?>